package cn.donting.jme.expansion.attribute.lmpl;

import com.jme3.math.ColorRGBA;
import org.jdesktop.swingx.JXColorSelectionButton;

import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import java.awt.*;


public class ColorAttribute extends AbsLineAttribute<ColorRGBA,JXColorSelectionButton> {


    public ColorAttribute(String title) {
        super(new JXColorSelectionButton());
        inputComponent.setPreferredSize(new Dimension(23,23));
        setTitle(title);
        inputComponent.getChooser().getSelectionModel().addChangeListener(new ChangeListener() {
            @Override
            public void stateChanged(ChangeEvent e) {
                valueChange(getValue());
            }
        });
    }

    @Override
    public void setValue(ColorRGBA value) {
        Color color = new Color((int) (value.r * 255), (int) (value.g * 255), (int) (value.b * 255), (int) (value.a * 255));
        inputComponent.getChooser().setColor(color);
        inputComponent.setBackground(color);
    }

    @Override
    public ColorRGBA getInputValue() {
        Color color = inputComponent.getChooser().getColor();
        return ColorRGBA.fromRGBA255(color.getRed(), color.getGreen(), color.getBlue(), color.getAlpha());
    }



}
